/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.accessorgen;

import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.field.FieldGenerator;
import com.inspur.edp.caf.generator.item.ClassGenerator;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.entity.accessor.base.ReadonlyDataException;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccGetResInfoGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccessorCreateNewGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccessorDataGetValueGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccessorDataSetValueGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.NewChildCollectionGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.base.AccessorInitializeGen;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.base.AcsCopyGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.DynamicPropAcsPropertyGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.InnerDataGeneratpr;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.NestedAccessorFieldGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.ReadonlyAccessorPropertyGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.ReadonlyNestedAccessorPropertyGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.ReadonlyPropGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.BaseDataGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.DataTypeImpContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityGeneratorContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property.BaseTypePropGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property.DataTypePropGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property.DynamicPropSetPropertyGenerator;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.eclipse.jdt.core.dom.Modifier;

public abstract class ReadonlyBaseAcsGenerator extends BaseDataGenerator
{
	protected ReadonlyBaseAcsGenerator(EntityGeneratorContext parContext, String nodeCode, MediateType targetInterfaceType)
	{
		super(parContext, nodeCode, targetInterfaceType);
		addTypes(nodeCode);
	}

	protected ReadonlyBaseAcsGenerator(EntityGeneratorContext parContext, IGspCommonDataType node, MediateType targetInterfaceType) {
		super(parContext, node, targetInterfaceType);
		addTypes(node.getCode());
	}

	private void addTypes(String nodeCode) {
		if (addToMediateTypes()) {
			ParentContext.getProjContext().getReadonlyAccessorMediateTypes().put(nodeCode, new MediateType( "",getName()));
		}
	}

	protected boolean addToMediateTypes(){return true;}
	//#region Props

	@Override
	protected List<BaseTypePropGenerator> getBaseTypePropGenerators(MediatePropertyInfo propertyInfo)
	{
		List<BaseTypePropGenerator> baseTypePropGenerators = new ArrayList<>();
		baseTypePropGenerators.add(ReadonlyAccessorPropertyGenerator.createImplicit(propertyInfo));
		if(Character.isLowerCase(propertyInfo.getPropertyName().charAt(0)) && !propertyInfo.getExtendInfos().containsKey("AssoUdtAtrribute"))
		{
			baseTypePropGenerators.add(ReadonlyAccessorPropertyGenerator.createImplicit(propertyInfo, true));
		}
		return baseTypePropGenerators;
	}

	@Override
	protected void addExtendPropertyGenerator(java.util.ArrayList<ClassPropertyGenerator> proGenerators)
	{
		InnerDataGeneratpr innerDataGen = new InnerDataGeneratpr(TargetInterfaceType);
		proGenerators.add(innerDataGen);
	}


	@Override
	protected ArrayList<Modifier.ModifierKeyword> getAccessModifier() {
		ArrayList<Modifier.ModifierKeyword> list =new ArrayList<>();
		list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
		return list;
	}

	@Override
	protected String getName()
	{
		//return String.format("%1$s_%2$sReadonlyAccessor", GeneratorUtil.getName( ParentContext.getProjContext().getCefConfig().getID()),GeneratorUtil.remove(TargetInterfaceType.getSimpleName()));
		return String.format("%1$sRDAcc", GeneratorUtil.remove(TargetInterfaceType.getClassName()));
	}

	@Override
	protected ArrayList<TypeInfo> addParentInterfaces() {
		ArrayList<TypeInfo> list = new ArrayList<>();
		TypeInfo info=new TypeInfo(TargetInterfaceType.getFullName());
		info.setUseFullName(true);
		list.add(info);
//		for (Class type : ExtendTypes)
//		{
//			TypeInfo  info1=new TypeInfo(type.getTypeName());
//			info1.setUseFullName(true);
//			list.add(info1);
//		}
		return list;
	}

	//#region method
	@Override
	protected java.util.ArrayList<ClassMethodGenerator> createMethodGenerators()
	{
		ParentContext.getProjContext().getCefGenContext().getCoreGenContext().addImport(ReadonlyDataException.class.getName());
		ParentContext.getProjContext().getCefGenContext().getCoreGenContext().addImport(HashMap.class.getName());
		ArrayList<ClassMethodGenerator> methods = super.createMethodGenerators();
		if (methods == null) {
			methods = new java.util.ArrayList<ClassMethodGenerator>();
		}

		ReadonlyPropGenerator readOnlyGen = new ReadonlyPropGenerator(true);
		methods.add(readOnlyGen);
		ClassMethodGenerator getValueGen = getAccessorDataGetValueGenerator(PropertyInfos);
		ClassMethodGenerator setValueGen = getAccessorDataSetValueGenerator(PropertyInfos);
		methods.add(getValueGen);
		methods.add(setValueGen);
		methods.add(getInitilizeGenerator());
		methods.add(getRestGenerator());
		methods.add(new AccessorCreateNewGenerator(getName()));
		addAcsExtendMethod(methods);
		ClassMethodGenerator resGen = get_GetResInfo_Generator();
		if(resGen != null) {
			methods.add(resGen);
		}
		return methods;
	}

	protected  ClassMethodGenerator getAccessorDataGetValueGenerator(List<MediatePropertyInfo> propertyInfos)
	{
		return new AccessorDataGetValueGenerator(ParentContext,NodeCode);
	}

	protected  ClassMethodGenerator getAccessorDataSetValueGenerator(List<MediatePropertyInfo> propertyInfos)
	{
		return new AccessorDataSetValueGenerator(ParentContext,NodeCode);
	}

	public AcsCopyGenerator getRestGenerator(){return new AcsCopyGenerator(getName(), NodeCode,PropertyInfos, ParentContext);}

	protected void addAcsExtendMethod(java.util.ArrayList<ClassMethodGenerator> methodGens) {

	}


	public abstract AccessorInitializeGen getInitilizeGenerator();
		//#endregion

//	@Override
//	protected java.util.ArrayList<FieldGenerator> createFieldGenerator()
//	{
//		ArrayList<FieldGenerator> list = super.createFieldGenerator();
//		if (list == null) {
//			list = new java.util.ArrayList<FieldGenerator>();
//		}
//		for (MediatePropertyInfo field:PropertyInfos) {
//			if(field.getElement() == null){
//				continue;
//			}
//			list.add(new NestedAccessorFieldGenerator(NodeCode,field.getElement(),ParentContext));
//		}
//		return list;
//	}

	@Override
	protected DataTypePropGenerator getDataTypePropGenerator(MediatePropertyInfo propertyInfo,
			String fieldName)
	{
		return new ReadonlyNestedAccessorPropertyGenerator(propertyInfo);
	}

	@Override
	protected void addEnrichedAssoUdtGenerator(ArrayList<BaseGenerator> list, MediatePropertyInfo propInfo)
	{
//		MediateType udtType = new MediateType(null, Utils
//				.getUdtType(propInfo.getElement().getUdtPkgName(), propInfo.getElement().getUdtID(),
//						ParentContext.getProjContext().getCefGenContext()).getTypeFullName());
		list.add(new EnrichedAssoUdtReadonlyAcsGenerator(ParentContext,  propInfo, getName()));
	}

	@Override
	protected DynamicPropSetPropertyGenerator getDynamicPropSetPropertyGenerator(
			MediatePropertyInfo propertyInfo) {
		return
				new DynamicPropAcsPropertyGenerator(propertyInfo);
	}

	protected ClassMethodGenerator get_GetResInfo_Generator(){
		if(ParentContext.getProjContext().getEntityMediateTypes().get(NodeCode) != null) {
			return new AccGetResInfoGenerator(ParentContext, NodeCode);
		}
		return null;
	}
}
